package com.nandi.deviceinstall.presenter

import android.annotation.SuppressLint
import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.*
import com.nandi.deviceinstall.contract.EquipmentBindContract
import com.nandi.deviceinstall.model.EquipmentBindModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RetrofitClient
import com.nandi.deviceinstall.net.RxScheduler
import com.nandi.deviceinstall.net.dto.BindAddDTO
import com.nandi.deviceinstall.net.dto.BindUpdateDTO
import com.uber.autodispose.FlowableSubscribeProxy
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import org.reactivestreams.Subscription
import java.io.File

class EquipmentBindPresenter: BasePresenter<EquipmentBindContract.View>(),EquipmentBindContract.Presenter {
    override fun requestDisName(token: String, disasterName: String, town: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeListDisName(token, disasterName,town)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNameBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNameBean>(mView!!) {
                    override fun onNext(t: DisNameBean) {
                        mView!!.handlerDisNameResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestUpdateBind(token: String, params: BindUpdateDTO) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeUpdateBind(token, params)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<BaseObjectBean<BindAddBean>>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<BaseObjectBean<BindAddBean>>(mView!!) {
                    override fun onNext(t: BaseObjectBean<BindAddBean>) {
                        mView!!.handlerUpdateBindResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    private val apiService = RetrofitClient.getInstance()!!.api!!

    private val model: EquipmentBindContract.Model

    init {
        model = EquipmentBindModel()
    }

    override fun requestRemoveDevPicFile(token: String, path: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeRemoveDevPicFile(token, path)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DevPicRemoveBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DevPicRemoveBean>(mView!!) {
                    override fun onNext(t: DevPicRemoveBean) {
                        mView!!.removeDevPicFile(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestAddBind(token: String, params: BindAddDTO) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeAddBind(token, params)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<BaseObjectBean<BindAddBean>>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<BaseObjectBean<BindAddBean>>(mView!!) {
                    override fun onNext(t: BaseObjectBean<BindAddBean>) {
                        mView!!.handlerAddBindResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    @SuppressLint("CheckResult")
    override fun requestUploadDevPic(token: String, file: File) {
        /*var fileName = ""
        var resultPath = ""*/
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeUploadDevPic(token, file)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<UploadPicBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<UploadPicBean>(mView!!) {
                    override fun onNext(t: UploadPicBean) {
                        mView!!.uploadPicSuccess(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
                /*.subscribeOn(Schedulers.io())
                .flatMap { t ->
                    fileName = t.data.substring(t.data.lastIndexOf("/") + 1)
                    resultPath = t.data
                    apiService.downPic(token,t.data,6)
                }
                .observeOn(AndroidSchedulers.mainThread())
                .`as`<FlowableSubscribeProxy<ResponseBody>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ResponseBody>(mView!!) {
                    override fun onNext(t: ResponseBody) {
                        //mView!!.uploadPicSuccess(t)
                        mView!!.downPicSuccess(t,fileName,resultPath)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })*/
    }

    override fun requestListDevFactory(token: String,factory: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDevFactory(token,factory)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DeviceFacBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DeviceFacBean>(mView!!) {
                    override fun onNext(t: DeviceFacBean) {
                        mView!!.handlerDevFactory(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListMonitorNum(token: String, unifiedCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListMonitorNum(token,unifiedCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ListMonitorNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ListMonitorNumBean>(mView!!) {
                    override fun onNext(t: ListMonitorNumBean) {
                        mView!!.handlerMonitorNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestDisNum(token: String, disName: String, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDisNum(token,disName,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNumBean>(mView!!) {
                    override fun onNext(t: DisNumBean) {
                        mView!!.handlerDisNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListDisaster(token: String,areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDisaster(token,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisasterBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisasterBean>(mView!!) {
                    override fun onNext(t: DisasterBean) {
                        mView!!.handlerListDisasterResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListTown(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListTown(token, id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TownBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TownBean>(mView!!) {
                    override fun onNext(t: TownBean) {
                        mView!!.handlerListTownResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListCity(token: String, district: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListCity(token, district)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<CityBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<CityBean>(mView!!) {
                    override fun onNext(t: CityBean) {
                        mView!!.handlerListCityResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}